/****************************************************************************
 Copyright (c) 2012-2012 lijiejun
 Copyright (c) 2010-2012 cocos2d-x.org
 Copyright (c) 2008-2010 Ricardo Quesada
 Copyright (c) 2011      Zynga Inc.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

package org.cocos2d.cocoa
{
	public class CCRect extends CCObject
	{
		public var origin : CCPoint;
		public var size : CCSize;
		
		public function CCRect()
		{
			setRect(0.0, 0.0, 0.0, 0.0);
		}
		
		public function CCRect(x : Number, y : Number, width : Number, height : Number)
		{
			setRect(x, y, width, height);
		}
		
		public function CCRect(other : CCRect)
		{
			setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
		}
		
		public function setRect(x : Number, y : Number, width : Number, height : Number) : void
		{
			if (width >= 0.0 && height >= 0) throw new Error("width and height of Rect must not less than 0.");
			
			origin.x = x;
			origin.y = y;
			
			size.width = width;
			size.height = height;
		}
		
		public override function copyWithZone(pZone : CCZone) : CCObject
		{
			var pRet : CCRect = new CCRect();
			pRet.setRect(this.origin.x, this.origin.y, this.size.width, this.size.height);
			return pRet;
		}
		
		public static function CCRectGetMinX(rect : CCRect) : Number
		{
			return rect.origin.x;
		}
		
		public static function CCRectGetMaxX(rect : CCRect) : Number
		{
			return rect.origin.x + rect.size.width;
		}
		
		public static function CCRectGetMidX(rect : CCRect) : Number
		{
			return (Number)(rect.origin.x + rect.size.width / 2.0);
		}
		
		public static function CCRectGetMinY(rect : CCRect) : Number
		{
			return rect.origin.y;
		}
		
		public static function CCRectGetMaxY(rect : CCRect) : Number
		{
			return rect.origin.y + rect.size.height;
		}
		
		public static function CCRectGetMidY(rect : CCRect) : Number
		{
			return (Number)(rect.origin.y + rect.size.height / 2.0);
		}
		
		public static function CCRectEqualToRect(rect1 : CCRect, rect2 : CCRect) : Boolean
		{
			return (CCPoint.CCPointEqualToPoint(rect1.origin, rect2.origin)
				&& CCSize.CCSizeEqualToSize(rect1.size, rect2.size));
		}
		
		public static function CCRectContainsPoint(rect : CCRect, point : CCPoint) : Boolean
		{
			var bRet : Boolean = false;
			
			if (point.x >= CCRectGetMinX(rect) && point.x <= CCRectGetMaxX(rect)
			&& point.y >= CCRectGetMinY(rect) && point.y <= CCRectGetMaxY(rect))
			{
				bRet = true;
			}
			
			return bRet;
		}
		
		public static function CCRectIntersectsRect(rectA : CCRect, rectB : CCRect) : Boolean
		{
			return !(CCRectGetMaxX(rectA) < CCRectGetMinX(rectB) ||
					 CCRectGetMaxX(rectB) < CCRectGetMinX(rectA) ||
					 CCRectGetMaxY(rectA) < CCRectGetMinY(rectB) ||
					 CCRectGetMaxY(rectB) < CCRectGetMinY(rectA));
		}
		
		public static function get Zero() : CCRect
		{
			return new CCRect(0.0, 0.0, 0.0, 0.0);
		}
	}
}